
TOOL.Category		= "Wire - Antennas"
TOOL.Name			= "Receiver Antenna"
TOOL.Command		= nil
TOOL.ConfigName		= ""

if ( CLIENT ) then
    language.Add( "Tool_wire_antenna_rx_name", "Receiver Antenna Tool (Wire)" )
    language.Add( "Tool_wire_antenna_rx_desc", "Spawns a receiver antenna for use with the wire system." )
    language.Add( "Tool_wire_antenna_rx_0", "Primary: Create/Update Receiver Antenna" )
	language.Add( "sboxlimit_wire_rx_antennas", "You've hit receiver antennas limit!" )
	language.Add( "undone_wireantennarx", "Undone Wire Receiver Antenna" )
else
	CreateConVar('sbox_maxwire_rx_antennas', 9000)
end

cleanup.Register( "wire_antenna_rxs" )
TOOL.Model = "models/radio/ra_cell_tower1.mdl"

function TOOL:LeftClick( trace )
	if trace.Entity && trace.Entity:IsPlayer() then return false end
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	if (CLIENT) then return true end
	local ply = self:GetOwner()
	if ( !self:GetSWEP():CheckLimit( "wire_antenna_rxs" ) ) then return false end
	if (not util.IsValidModel(self.Model)) then return false end
	if (not util.IsValidProp(self.Model)) then return false end	
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	wire_antenna_rx = MakeAntennaRX( ply, Ang, trace.HitPos )
	local min = wire_antenna_rx:OBBMins()
	wire_antenna_rx:SetPos( trace.HitPos - trace.HitNormal * min.z )
	local const = WireLib.Weld(wire_antenna_rx, trace.Entity, trace.PhysicsBone, true)
	wire_antenna_rx:GetPhysicsObject():EnableCollisions(true)
	undo.Create("WireAntenna")
		undo.AddEntity( wire_antenna_rx )
		undo.AddEntity( const )
		undo.SetPlayer( ply )
	undo.Finish()
	ply:AddCleanup( "wire_antenna_rxs", wire_antenna_rx )
	ply:AddCleanup( "wire_antenna_rxs", const )
	ply:AddCleanup( "wire_antenna_rxs", nocollide )
	return true
end

if (SERVER) then
	function MakeAntennaRX( pl, Ang, Pos)
		if ( !pl:CheckLimit( "wire_antenna_rxs" ) ) then return false end
		local wire_antenna_rx = ents.Create( "gmod_wire_radio_receiver" )
		if (!wire_antenna_rx:IsValid()) then return false end
		wire_antenna_rx:SetAngles( Ang )
		wire_antenna_rx:SetPos( Pos )
		wire_antenna_rx:Spawn()
		wire_antenna_rx:SetPlayer( pl )
		pl:AddCount( "wire_antenna_rxs", wire_antenna_rx )
		return wire_antenna_rx
	end
	duplicator.RegisterEntityClass("gmod_wire_antenna_rx", MakeAntennaRX, "Ang", "Pos")
end

function TOOL:UpdateGhostAntenna( ent, player )
	if ( !ent ) then return end
	if ( !ent:IsValid() ) then return end
	local tr = utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
	ent:SetAngles( Ang )
	ent:SetNoDraw( false )
end


function TOOL:Think()
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != self.Model ) then
		self:MakeGhostEntity( self.Model, Vector(0,0,0), Angle(0,0,0) )
	end
	self:UpdateGhostAntenna( self.GhostEntity, self:GetOwner() )
end

function TOOL.BuildCPanel(panel)
	panel:AddControl("Header", { Text = "#Tool_wire_antenna_rx_name", Description = "#Tool_wire_antenna_rx_desc" })
end
